package com.ieheima.ai.controller;


import com.ieheima.ai.memory.ChatMap;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.codec.AbstractCharSequenceDecoder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/ai")
public class ChatController {


    @Qualifier("chatClient") // 指定注入的 Bean 名称
    private final ChatClient chatClient;

    @Qualifier("chatClient2") // 指定注入的 Bean 名称
    private final ChatClient chatClient2;

    private final ChatMap chatMap;


    @RequestMapping(value = "/chat", produces = "text/html;charset=utf-8") //, produces = "text/html;charset=utf-8"
    public String chat(String prompt, @RequestParam(value = "chatId") String conversationId)
    {
        chatMap.save("chat", conversationId);
        // log.info(prompt);
        String s=chatClient2.prompt()
                .user(prompt)
                .advisors(a->a.param("conversationId", conversationId))  //
                .call()   // 阻塞式调用
                .content();

        return s;
    }

    @RequestMapping(value = "/chat_s", produces = "text/html;charset=utf-8") //, produces = "text/html;charset=utf-8"
    public Flux<String> chat_s(@RequestParam String prompt)
    {

        Flux<String> s = chatClient2.prompt()
                .user(prompt)
                .stream().content();
        return s;
    }
}
